package com.newangels.agent;

import com.newangels.behaviour.AgentManageBehaviour;
import com.newangels.service.AgentManagerService;
import com.newangels.util.MyInvocationHandler;
import jade.core.Agent;

import java.lang.reflect.Proxy;

import static com.newangels.service.impl.AgentManagerServiceImpl.getAgentManagerServiceImpl;

/**
 * 测试Agent
 *
 * @author: TangLiang
 * @date: 2020/5/11 17:11
 * @since: 1.0
 */
public class TestAgent extends Agent {
    private AgentManageBehaviour agentManageBehaviour = AgentManageBehaviour.getAgentManageBehaviour(this);
    private AgentManagerService agentManagerService = getAgentManagerServiceImpl();
    MyInvocationHandler handler = new MyInvocationHandler(agentManagerService);
    AgentManagerService proxy = (AgentManagerService) Proxy.newProxyInstance(agentManagerService.getClass().getClassLoader(), agentManagerService.getClass().getInterfaces(), handler);

    @Override
    protected void setup() {
        Object[] obj = getArguments();
        proxy.updateSTATE(this.getLocalName(), "1");
        //agentManagerService.updateSTATE(this.getLocalName(),"1");
        //添加行为
        addBehaviour(agentManageBehaviour);
    }

    @Override
    protected void takeDown() {
        //修改数据库Agent状态
        proxy.updateSTATE(this.getLocalName(), "0");
        if (agentManageBehaviour != null) removeBehaviour(agentManageBehaviour);
    }
}
